
function add(str){
    var date=new Date();
    $(".bottom").hide();
    $(".div_add").show();
    $(".content-left").show();
    $(".content-left-1").hide();
    $("#add_title").html(str);
    $('input[type=date]').val(date.Format('yyyy-MM-dd'));
    $("#sbmt").click(function () {
        var data={};
        var price=$("#price").val();
        var details=$("#details").val();
        date=$("#date").val();
        var isUpdate=verifyDate(date);
        switch (str) {
            case '餐饮':
                str='eat';
                data={
                    'str':str,
                    'eat':price,
                    'eatDetail':details,
                    'day':date
                };
                break;
            case '住房':
                str='house';
                data={
                    'str':str,
                    'house':price,
                    'houseDetail':details,
                    'day':date
                };
                break;
            case '通讯':
                str='communication';
                data={
                    'str':str,
                    'communication':price,
                    'communicationDetail':details,
                    'day':date
                };
                break;
            case '交通':
                str='traffic';
                data={
                    'str':str,
                    'traffic':price,
                    'trafficDetail':details,
                    'day':date
                };
                break;
            case '日用':
                str='daily';
                data={
                    'str':str,
                    'daily':price,
                    'dailyDetail':details,
                    'day':date
                };
                break;
            case '学习':
                str='education';
                data={
                    'str':str,
                    'education':price,
                    'educationDetail':details,
                    'day':date
                };
                break;
            case '服饰':
                str='clothes';
                data={
                    'str':str,
                    'clothes':price,
                    'clothesDetail':details,
                    'day':date
                };
                break;
            case '零食':
                str='snacks';
                data={
                    'str':str,
                    'snacks':price,
                    'snacksDetail':details,
                    'day':date
                };
                break;
            case '水果':
                str='fruits';
                data={
                    'str':str,
                    'fruits':price,
                    'fruitsDetail':details,
                    'day':date
                };
                break;
            case '医疗':
                str='medical';
                data={
                    'str':str,
                    'medical':price,
                    'medicalDetail':details,
                    'day':date
                };
                break;
            case '其它':
                str='others';
                data={
                    'str':str,
                    'others':price,
                    'othersDetail':details,
                    'day':date
                };
        }
        if(!isUpdate){
            addNew(data);
        }else{
            update(data);
        }
    })
}
function update(data) {
    $.ajax({
        url:URL_PREFIX+"/cost/updateByDate",
        type:"put",
        data:JSON.stringify(data),
        contentType: "application/json",
        statusCode:{
            200:function () {
                window.location="manage.html";
            },403:function () {
                gotoLogin();
            }
        }
    })
}
function addNew(data) {
    $.ajax({
        url:URL_PREFIX+"/cost/add",
        type:"post",
        data:JSON.stringify(data),
        contentType:"application/json",
        statusCode:{
            200:function () {
                window.location="manage.html";
            },403:function () {
                gotoLogin();
            }
        }
    })
}
function addE() {
    $(".bottom").hide();
    $(".div_add").show();
    $(".content-left").hide();
    $(".content-left-1").show();
    $("#add_title").html('发生的事件');
    var date=new Date();
    $('input[type=date]').val(date.Format('yyyy-MM-dd'));
    $("#sbmt").click(function () {
        date=$("#date").val();
        var isUpdate=verifyDate(date);
        var data={
            'str':'todayEvent',
            'todayEvent':$("#details_today").val(),
            'day':date
        };
        if(!isUpdate){
            addNew(data);
        }else{
            update(data);
        }
    });
}
function queryPayList(year,month) {
    var sum=0;
    $.ajax({
        url:URL_PREFIX+"/cost/queryPayList/"+year+"/"+month,
        type:"get",
        dataType:"text json",
        async:false,
        statusCode:{
            200:function (data) {
                var str="";
                $("#tbody1").empty();
                $(data).each(function (index,val) {
                    var date=new Date(val.day);
                    str+="<tr>" +
                        "<td>"+val.day+"</td>" +
                        "<td title='"+val.eatDetail+"'>"+val.eat+"</td>" +
                        "<td title='"+val.houseDetail+"'>"+val.house+"</td>" +
                        "<td title='"+val.communicationDetail+"'>"+val.communication+"</td>" +
                        "<td title='"+val.trafficDetail+"'>"+val.traffic+"</td>" +
                        "<td title='"+val.dailyDetail+"'>"+val.daily+"</td>" +
                        "<td title='"+val.educationDetail+"'>"+val.education+"</td>" +
                        "<td title='"+val.clothesDetail+"'>"+val.clothes+"</td>" +
                        "<td title='"+val.snacksDetail+"'>"+val.snacks+"</td>" +
                        "<td title='"+val.fruitsDetail+"'>"+val.fruits+"</td>" +
                        "<td title='"+val.medicalDetail+"'>"+val.medical+"</td>" +
                        "<td title='"+val.othersDetail+"'>"+val.others+"</td>" +
                        "<td title='"+val.todayEvent+"'>"+val.sum+"</td>" +
                        "<td>"+
                        "<a class='a_del' href='javascript:del(\""+val.day+"\")'>删除</td>" +
                        "</tr>";

                    sum+=val.sum;
                });
                $("#tbody1").append(str);
            },403:function () {
                gotoLogin();
            }
        }
    });
    return parseFloat(sum).toFixed(2);
}
function gotoLogin() {
    window.location="login.html";
}
function del(day){
    $.ajax({
        url:URL_PREFIX+"/cost/del/"+day,
        type:"delete",
        statusCode:{
            200:function () {
                window.location="manage.html";
            },403:function () {
                gotoLogin();
            }
        }
    })
}

// function getMonthTotalPay(year,month,selector) {
//     $.ajax({
//         url:"http://localhost:8081/cost/getTotalPay/"+year+"/"+month,
//         type:"get",
//         statusCode:{
//             200:function (totalPay) {
//                 selector.html(totalPay);
//             }
//         }
//     })
// }
function cancel() {
    $(".div_add").hide();
    $(".bottom").show();
    // window.location="manage.html";
    $("#details").val("");
    $("#price").val(0);
    $("#details_today").val("");
}
function query() {
    var year=$("#sel_year").val();
    var month=$("#sel_month").val();
    var selector_month=$("#month");
    if(year==0 && month!=0){
        alert("请选择年份！");
        return;
    }
    if(year==0 && month==0){
        selector_month.html('所有');
    }else if(year!=0 && month==0){
        selector_month.html(year+'年');
    }else{
        selector_month.html(year+'年'+month+'月');
    }
    var sum=queryPayList(year,month);
    $("#other-month-pay").html(sum);
    getAvg(year,month,0,0,$("#other-month-avg"));
}
function cleanSel() {
    $("#sel_year").val(0);
    $("#sel_month").val(0);
}
function getAvg(year,month,day,targetDays,selector){
    var date={
        "year":year,
        "month":month,
        "day":day,
        "targetDays":targetDays
    };
    $.ajax({
        url:URL_PREFIX+"/cost/getXDaysAvg",
        type:"post",
        data:JSON.stringify(date),
        contentType:"application/json",
        dataType:"text",
        statusCode:{
            200:function (avg) {
                selector.html(parseFloat(avg).toFixed(2));
            }
        }
    })
}
$(function () {
    $(".bottom").show();
    $(".div_add").hide();
    $("#tb").show();
    $("#show_all").hide();
    $("#show_all").click(function () {
        $("#tb").show();
        $("#show_all").hide();
        $("#hide_all").show();
    });
    $("#hide_all").click(function () {
        $("#tb").hide();
        $("#hide_all").hide();
        $("#show_all").show();
    });

    var date=new Date();
    var year=date.getFullYear();
    var month=date.getMonth()+1;
    var day=date.getDate();
    $("#month").html(year+'年'+month+'月');
    var sum=queryPayList(year,month);
    //查看本月支出
    $("#current-month-pay").html(sum);
    getAvg(year,month,0,0,$("#current-month-avg"));
    getAvg(year,month,day,7,$("#7-days-avg"));
    getAvg(year,month,day,30,$("#30-days-avg"));
});
